/**
 * project name: anycs,
 * copyright: maximus zhang,
 * License: MIT-LICENSE,
 * contact: maximus.zhang@gmail.com,
 * open-source: http://anycs.googlecode.com/svn/trunk/anycs.js,
 *
 */
function anycsmask(){
    var me=document, _=this;
    _.mask=me.getElementById("anycsmask") || me.body.insertBefore(me.createElement("div"),me.body.firstChild);
    _.mask.id="anycsmask";
    _.mask.style.cssText="position:fixed;top:0;left:0;width:100%;height:100%;border:0;filter:alpha(opacity=50);opacity:0.5;z-index:9898;background:#ddd;display:none;";

    _.show=function(){
        _.mask.style.display = "";
    return _;
    };

    _.hide = function(){
        _.mask.style.display = "none";
    return _;
    };
}
(function anycs(){
    var A = arguments, me=document, _ = this;
    _.bind=function (o, evt, fn, cb){
        var aE = 'attachEvent',
            aEL = 'addEventListener';
        if ( !o[aE] && o[aEL] ) {
            return o[aEL](evt, fn, !!cb);
        }
    return o[aE]('on'+ evt, fn);
    }

    _.unbind=function (o, evt, fn, cb){
        var dE = 'detachEvent',
            rEL = 'removeEventListener';
        if (!o[dE] && o[rEL]) {
            return o[rEL](evt, fn, !!cb);
        }
    return o[dE]('on'+ evt, fn);
    }
    _.hide = function(){
        _.mask && _.mask.hide();
        _.xwin.style.display = "none";
        _.shows();
    return _;
    };
    _.move=function(ev){
        var ev=window.event||ev;
        me.all && _.xtit.setCapture();
        _.pz.left = ev.clientX-_.pz._x<=0 || _.pz.W<=_.pz.width ? 0 : ev.clientX-_.pz._x+_.pz.width>=_.pz.W ? _.pz.W-_.pz.width : ev.clientX-_.pz._x;
        _.pz.top = ev.clientY-_.pz._y<=0 || _.pz.H<=_.pz.height ? 0 : ev.clientY-_.pz._y+_.pz.height>=_.pz.H ? _.pz.H-_.pz.height : ev.clientY-_.pz._y;
        _.xwin.style.top = _.pz.top +'px';
        _.xwin.style.left = _.pz.left +'px';
    };
    _.stop = function(){
        _.unbind(me, 'mousemove', _.move);
        _.unbind(me, 'mouseup', _.stop);
        me.all && _.xtit.releaseCapture();
    };

    _.tomove = function(ev){
        var ev =window.event||ev;
        _.pz._y = ev.clientY-_.pz.top;
        _.pz._x = ev.clientX-_.pz.left;
        me.selection&&me.selection.empty();
        _.bind(me, 'mousemove', _.move);
        _.bind(me, 'mouseup', _.stop);
    };

    _.pos=function(el){
        var t=el.offsetTop,
            l = el.offsetLeft,
            w=el.offsetWidth,
            h=el.offsetHeight,
            win = me.compatMode && me.compatMode!='BackCompat' ? me.documentElement : me.body;
        while ( el=el.offsetParent ) {
            l += el.offsetLeft;
            t += el.offsetTop;
        }
    return {"top":t,"left":l,"width":w,"height":h,"oW":win.offsetWidth,"oH":win.offsetHeight,"W":win.clientWidth || self.innerWidth,"H":win.clientHeight || self.innerHeight};
    };

    _.shows=function () {
        _.xsid.style.display="";
    return _;
    };
    _.xsid=me.getElementById("anycside") || me.body.appendChild(me.createElement("div"));
    _.xsid.id="anycside";
    _.xsid.style.cssText="position:fixed;top:200px;right:20px;width:90px;height:150px;border:1px solid #def;background:#fff;display:none;-moz-border-radius:5px;-khtml-border-radius:5px;-webkit-border-radius:5px;border-radius:5px;";
    _.xwin=me.getElementById("anycswin") || me.body.appendChild(me.createElement("div"));
    _.xwin.id="anycswin";
    _.xwin.style.cssText="position:fixed;top:200px;left:200px;z-index:9998;width:400px;height:180px;border:1px solid #999;background:#fff;font-size:9pt;-moz-border-radius:5px;-khtml-border-radius:5px;-webkit-border-radius:5px;border-radius:5px";
    _.pz=_.pos(_.xwin);
    _.xwin.style.left=_.pz.W<_.pz.width?0:(_.pz.W-_.pz.width)/2 + "px";
    _.xwin.style.top=_.pz.H<_.pz.height?0:(_.pz.H-_.pz.height)/2 + "px";
    _.pz=_.pos(_.xwin);
    _.xtit=_.xwin.appendChild(me.createElement("div"));
    _.xtit.style.cssText="width:100%;height:28px;line-height:28px;border:0;background:url(/icos/inavbg28.gif) repeat-x;overflow:hidden;-moz-border-radius:5px 5px 0 0;-khtml-border-radius:5px 5px 0 0;-webkit-border-radius:5px 5px 0 0;border-radius:5px 5px 0 0;";
    _.bind(_.xtit, 'mousedown', _.tomove);
    _.xico=_.xtit.appendChild(me.createElement("div"));
    _.xico.style.cssText="float:left;width:16px;height:16px;margin:6px;background:url(/icos/emoticon_smile.png) no-repeat;";
    _.xtxt=_.xtit.appendChild(me.createElement("div"));
    _.xtxt.style.cssText="float:left;font-weight:bold;cursor:default;";
    _.xtxt.innerHTML="欢迎访问客服系统展示站点！";
    _.xico.onmousedown=_.xtxt.onmousedown=function(){me.selection&&me.selection.empty();return false;};
    _.xtitx=_.xwin.appendChild(me.createElement("div"));
    _.xtitx.style.cssText="cursor:pointer;width:17px;height:16px;margin:6px;position:absolute;z-index:9999;top:0;right:0;background:url(/icos/iclose.gif);";
    _.xtitx.onmouseover=function(){this.style.backgroundPosition="0 -16px";};
    _.xtitx.onmouseout=function(){this.style.backgroundPosition="0 0";};
    _.xtitx.onclick=_.hide;
    _.xbody=_.xwin.appendChild(me.createElement("div"));
    _.xbody.style.cssText="margin:10px auto;background:url(ivt_bg.gif) no-repeat;width:375px;height:130px;";
    _.xcont=_.xbody.appendChild(me.createElement("span"));
    _.xcont.style.cssText="float:right;clear:both;width:200px;height:60px;margin:20px 20px 0 0;";
    _.xcont.innerHTML="客服正在为您服务！请问有什么可以帮到您？";
    _.xcmd=_.xbody.appendChild(me.createElement("div"));
    _.xcmd.style.cssText="float:right;clear:right;margin:10px 16px 0 0;width:180px;";
    _.xopen=_.xcmd.appendChild(me.createElement("button"));
    _.xopen.innerHTML="开始交谈";
    _.xopen.style.cssText="line-height:21px;margin-right:10px;";
    _.xopen.onclick=function(){window.open("demo-guest.php","_blank","height=480,width=640,top="+ (screen.availHeight-580)/2 +",left="+ (screen.availWidth-640)/2 +",status=yes,toolbar=no,menubar=no,resizable=yes,scrollbars=no,location=no,titlebar=no",true);_.hide();};
    _.xnext=_.xcmd.appendChild(me.createElement("button"));
    _.xnext.innerHTML="下次再说";
    _.xnext.style.cssText="line-height:21px;";
    _.xnext.onclick=function(){_.hide();};
    _.mask=(new anycsmask()).show();
})();
